// miniprogram/pages/index/index.js

import Travel from "../../utils/travel";
import Util from "../../utils/util";

let currentPage = 0;
let limit = 20;
let where = {};

Page({

  /**
   * 页面的初始数据
   */
  data: {
    travelData: [],
    show: true,
    date: "",
    no: "",
    loadAll: 0,
    total: 0,
    endDate: Util.formatTime(new Date()) 
  },

  fetchListData: function(){
    wx.showLoading({
      title: '数据加载中'
    })

    Travel.search(where, currentPage, limit, res => {
      if (res.data && res.data.length > 0) {
        console.log("请求成功", res.data)
        //把新请求到的数据添加到dataList里  
        let travelData = this.data.travelData.concat(res.data);
        travelData.forEach(value=>{
          value.t_date = value.t_date.replace('2020-', '');
        })
        this.setData({
          travelData 
        });

      } else {
        this.setData({
          loadAll: 1, //把“没有数据”设为1，显示 
        });
      }
      wx.hideLoading();
    })
  },

  detailTap: function(e){
    let id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/pages/detail/index?id='+id
    })
  },

  dateChange: function (e) {
    // console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      date: e.detail.value
    })
  },

  noInput: function(e){
    let no = e.detail;
    console.log(e)
    this.setData({
      no
    })
  },

  searchTap: function(){
    let date = this.data.date;
    let no = this.data.no;

    if(date=="" && no=="")
    {
      wx.showToast({
        title: '请输入查询条件',
        icon: 'none'
      })

      return;
    }
    
    where = {
      date,
      no
    }

    this.setData({
      loadAll: 0,
      travelData: []
    })

    currentPage = 0;

    this.fetchListData();
    
  },

  resetTap: function(){
    this.setData({
      date: "",
      no: "",
      loadAll: 0,
      travelData: []
    })
    currentPage = 0;
    where = {};
    this.fetchListData();
  },

  scrolltolower: function(){
    console.log("滚动到底部时触发");
    console.log(where);
    currentPage++;
    this.fetchListData();
  },

  aboutMe: function(){
    wx.navigateTo({
      url: '/pages/about/index',
    })
  },

  // scrolltoupper: function(){
  //   console.log("滚动到顶部时触发");
  //   Travel.total(res => {
  //     console.log(res)
  //     this.setData({
  //       total: res.total
  //     })
  //   })
  //   this.setData({
  //     loadAll: 0,
  //     travelData: []
  //   });
  //   currentPage = 0;
  //   this.fetchListData();
  // },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

    console.log(this.data.endDate)

    wx.getSystemInfo({
      success: (res) => {
        console.log(res)
        if (res.model == 'iPhone X' || res.model == 'iPhone XS Max' || res.model == 'iPhone XR') {
          this.setData({
            isIphoneX: true
          })
        }
      }
    });
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    Travel.total(res => {
      console.log(res)
      this.setData({
        total: res.total
      })
    })
    currentPage = 0;
    this.fetchListData();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    Travel.total(res => {
      console.log(res)
      this.setData({
        total: res.total
      })
    })
    this.setData({
      loadAll: 0,
      travelData: [] 
    });
    currentPage = 0;
    this.fetchListData();
    wx.stopPullDownRefresh();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})